﻿using BackData;
using CSRedis;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis.Differencing;
using RBAC.API.Command.User;
using RBAC.API.DTOs;
using System.Collections.Generic;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace RBAC.API.Controllers
{
    /// <summary>
    /// 用户控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class UsersController : ControllerBase
    {
        IMediator mediator;
        private readonly CSRedisClient cSRedisClient;

        public UsersController(IMediator mediator ,CSRedisClient cSRedisClient)
        {
            this.mediator = mediator;
            this.cSRedisClient = cSRedisClient;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="request">账号密码</param>
        /// <param name="cancellationToken"></param>
        /// <returns>用户信息</returns>
        [HttpGet]     
        [AllowAnonymous]
        public Task<ApiResult<LoginDTO>> UserLogin([FromQuery]UserLoginCommand request)
        {

            return mediator.Send(request);
        }
        /// <summary>
        /// 用来测试token的方法
        /// </summary>
        /// <returns></returns>
        [HttpPost]
     
        public string TestJWT()
        {


            int baseExpire = 1800; // 30分钟
            int randomOffset = new Random().Next(300); // 0~5分钟随机
         

           // cSRedisClient.HSet("list", "Phone", "17537913863");
           // cSRedisClient.Expire("list", baseExpire);  //列表三十分钟后过期
           //cSRedisClient.Set("测试", "Test",baseExpire + randomOffset); //五分钟后过期
            return "测试成功";
        }
        /// <summary>
        /// 获取用户分页信息
        /// </summary>
        /// <param name="request">参数</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>用户分页查询列表</returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
      //  [AllowAnonymous]
        public Task<ApiPaging<UserDTO>> UserGet([FromQuery]UserGetCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> UserDel(UserDelCommand request)
        {
            return await mediator.Send(request);
        }


        /// <summary>
        /// 添加用户和用户配置的角色
        /// </summary>
        /// <param name="request">参数</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<ApiResult<int>> UserAdd(UserAddCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 用户信息修改
        /// </summary>
        /// <param name="request">参数</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>影响行数</returns>
        [HttpPut]
        public async Task<ApiResult<int>> UserUpdate(UserUpdateCommand request)
        {
            return await mediator.Send(request);
        }
    }
}
